﻿<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { BasicTableColumn } from '@/types'
import {
  Purchase,
  PurchaseAddType,
  ConditionSelectorParams
} from '@/types/modules/purchase'
import { formatToDateTime } from '@/utils/dateUtil'
import { useModal } from '@/hooks/system/useModal'
import { BasicModal as BModal } from '@/components/Modal'
import { useDrawer } from '@/hooks/system/useDrawer'
import { getEnumDisplayName } from '@/utils/enum'
import { PurchaseStatus, purchaseStatusOptions } from '@/types/modules/purchase-status'
import CommiterDetail from '@/views/user/detail.vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useHttp } from '@/hooks/web/useHttp'
import { useTableList } from '@/hooks/system/useTableList'
import * as api from '@/api/modules/purchase'
import search from './components/search.vue'
import list from './components/list.vue'
import add from './add.vue'
import detail from './detail.vue'

const { t } = useI18n()

const searchParams: ConditionSelectorParams = reactive({})

let basicData: BasicTableColumn<Purchase>[] = [
  {
    title: '采购事由',
    dataIndex: 'remark'
  },
  {
    title: '申请人',
    dataIndex: 'commiter',
    isCheck: true,
    name: 'name',
    ellipsis: true
  },
  {
    title: '采购状态',
    dataIndex: 'status',
    customRender: data =>
      getEnumDisplayName(data.record.status, purchaseStatusOptions)
  },
]

const { columns, pageParams, tableData, total, getData } =
  useTableList<Purchase>(basicData)

onMounted(() => {
  getDataList()
})

const getDataList = getData<ConditionSelectorParams>(api.getConditionSelector, searchParams)

const onSearch = () => {
  pageParams.value.pageNum = 1
  getDataList()
}

const addFormRef = ref()
const {
  bindValue: addBindValue,
  register: addRegister,
  onShow: onShowAdd,
  onClose: onCloseAdd,
  getInnerForm: getAddForm
} = useModal<Purchase>()
const handleAddResult = useHttp(
  t('system.api.addError'),
  t('system.api.addSuccess')
)

const onFinishAdd = async function () {
  const result = await getAddForm<PurchaseAddType>(addFormRef)
  if (result) {
    const res = await api.createPurchase(result)
    handleAddResult(res, () => {
      getDataList()
      onCloseAdd()
    })
  }
}

const {
  bindValue: detailBindValue,
  register: detailRegister,
  currentItem: detailCurrentItem,
  onShow: onShowDetail,
  onClose: onCloseDetail
} = useModal<Purchase>()

const {
  visible: relateVisible,
  currentItem: relateCurrentItem,
  onShow: onShowRelate
} = useDrawer()

const relateComps = {
  commiter: CommiterDetail,
}

const relateAttrName = ref<string>('')
const relateTitle = ref<string>('')
const onCheckRelate = (attrName: string, title: string, relate: any) => {
  relateAttrName.value = attrName
  relateTitle.value = title
  onShowRelate(relate)
}
</script>

<template>
  <div class="page-wrapper">
    <a-card class="search-card">
      <search :formState="searchParams" @search="onSearch" />
    </a-card>
    <a-card class="table-card">
      <list v-bind="{ pageParams, total, columns, tableData }" title="资产采购" @add="onShowAdd" @refresh="getDataList"
        @check="onCheckRelate">
        <template #operate="row">
          <a type="link" @click="onShowDetail(row)">{{ t('system.detail') }}</a>
        </template>
      </list>
    </a-card>

    <b-modal v-bind="addBindValue" :title="t('system.modal.add')" @register="addRegister" @ok="onFinishAdd">
      <add ref="addFormRef" />
    </b-modal>

    <b-modal v-bind="detailBindValue" :footer="null" :title="t('system.modal.detail')" @register="detailRegister"
      @cancel="onCloseDetail">
      <detail :item-data="detailCurrentItem" />
    </b-modal>

    <a-drawer v-model:visible="relateVisible" :title="relateTitle" width="40vw" placement="right">
      <component :is="relateComps[relateAttrName]" :item-data="relateCurrentItem" />
    </a-drawer>
  </div>
</template>

<style lang="less" scoped>
.page-wrapper {
  padding: 20px;

  .table-card {
    margin-top: 20px;
  }
}

.drawer-footer {
  text-align: right;

  button {
    margin-left: 8px;
  }
}
</style>
